#include<stdio.h>
#include<stdlib.h>
#include"list.h"
void showmovies(Item item);
char *s_gets(char *st, int n);
int main(){
	List movies;
	Item tmp;
	InitList(&movies);
	if(ListFull(&movies)){
		fprintf(stderr, "No memory available!Bye!\n");
		exit(1);
	}
	puts("Enter first movie title:");
	while(s_gets(tmp.title, TSIZE) != NULL && tmp.title[0]!='\0'){
		puts("Enter your rating<0-10>:");
		scanf("%d", &tmp.rating);
		while(getchar()!='\n')continue;
		if(AddItem(tmp, &movies) == false){
			fprintf(stderr,"Proble allocating memory!\n");
			break;
		}
		if(ListFull(&movies)){
			puts("The list is now full.");
			break;
		}
		puts("Enter next movie title(empty line to stop):");
	}
	
	if(ListEmpty(&movies))printf("No data entered.");
	else{
		printf("Here is the movie list:\n");
		Trave(&movies, showmovies);
	}
	printf("You entered %d movies.\n", ListItemCount(&movies));

	FreeList(&movies);
	printf("Bye!\n");

	return 0;
}

void showmovies(Item item){
	printf("Movie: %s Rating: %d\n", item.title, item.rating);
}
char *s_gets(char *st, int n){
	char *retVal;
	char *find;
	retVal = fgets(st, n, stdin);
	if(retVal){
		find = strchr(st, '\n');
		if(find) *find='\0';
		else while(getchar()!='\n')continue;
	}
	return retVal;
}
